//
//  basic_case.h
//  GCDBenchmark
//
//  Created by 郭晓龙 on 2025/10/13.
//

#ifndef basic_case_h
#define basic_case_h

#include "pattern.h"
#include <unistd.h>

// Fork-Join 测试
inline void execute_fork_join_test(size_t task_type, size_t task_count, size_t task_duration, qos_class_t qos_class) {
    auto qos = QoSGenerator(qos_class);
    fork_join_pattern(task_type, task_count, task_duration, qos);
}

// Fibonacci 递归测试
inline void execute_fibonacci_test(size_t task_type, size_t task_count, size_t task_duration, qos_class_t qos_class) {
    auto qos = QoSGenerator(qos_class);
    int depth = (int)task_count;  // Fibonacci 使用 task_count 作为递归深度

    dispatch_group_t group = dispatch_group_create();
    fibonacci(depth, task_type, task_duration, qos, group);
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
}

// ARIAW NSOperation 测试
inline void execute_ariaw_nsoperation_test(size_t task_type, size_t buffer_count, size_t slice_count, size_t task_duration, qos_class_t qos_class) {
    auto qos = QoSGenerator(qos_class);
    ariaw_pattern_nsoperation(task_type, buffer_count, slice_count, task_duration, qos);
}

// ARIAW Semaphore 测试
inline void execute_ariaw_semaphore_test(size_t task_type, size_t buffer_count, size_t slice_count, size_t task_duration, qos_class_t qos_class) {
    auto qos = QoSGenerator(qos_class);
    ariaw_pattern_semaphore(task_type, buffer_count, slice_count, task_duration, qos);
}

// 串行队列测试
inline void execute_serial_queue_test(size_t task_type, size_t task_count, size_t task_duration, size_t submit_interval, qos_class_t qos_class) {
    SerialQueue queue(qos_class);

    for (size_t i = 0; i < task_count; i++) {
        if (submit_interval > 0 && i > 0) {
            usleep((useconds_t)submit_interval);
        }
        uint64_t tsc = arm64_cnt_vct();
        queue.Submit(^{
            TRACE_COUNT("cnt_delay", arm64_cnt_vct() - tsc);
            run_task(task_type, task_duration);
        }, qos_class);
    }

    queue.Wait();
}

// 并发队列测试
inline void execute_concurrent_queue_test(size_t task_type, size_t task_count, size_t task_duration, int max_concurrency, size_t submit_interval, qos_class_t qos_class) {
    ConcurrentQueue queue(qos_class, max_concurrency);
    for (size_t i = 0; i < task_count; i++) {
        if (submit_interval > 0 && i > 0) {
            usleep((useconds_t)submit_interval);
        }
        uint64_t tsc = arm64_cnt_vct();
        queue.Submit(^{
            TRACE_COUNT("cnt_delay", arm64_cnt_vct() - tsc);
            run_task(task_type, task_duration);
        }, qos_class);
    }

    queue.Wait();
}

// 周期性任务测试 - Updated for new parameter format: task_type, cnt, cost, qos
inline void execute_periodic_tasks_test(size_t task_type, size_t cnt, size_t cost, qos_class_t qos_class) {
    {
        Periodic(task_type, cnt, cost, qos_class).Run();
    }
}

#endif /* basic_case_h */
